﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;

namespace ERP.QuanlyKho
{
    public partial class viewERP_Xuatkho : DevExpress.XtraEditors.XtraForm
    {
        public cs_erp_hoadonInfo hoadon = new cs_erp_hoadonInfo();
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public cs_erp_hanghoaInfo hanghoa = new cs_erp_hanghoaInfo();
        public cs_erp_khohangInfo kho = new cs_erp_khohangInfo();
        public cs_erp_nhapkhoInfo nk = new cs_erp_nhapkhoInfo();
        int count = 0;
        public int flag = 1;

        public viewERP_Xuatkho()
        {
            InitializeComponent();
        }

        private void viewERP_Xuatkho_Load(object sender, EventArgs e)
        {
            this.Load_Combo();
            if (this.flag == 2)
            {
                this.Load_Info();
            }
        }

        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            ds = this.hanghoa.Get_List_Xuatkho(Convert.ToInt32(this.cboKhoxuat.EditValue.ToString().Trim()));
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
                this.gridView1.ExpandAllGroups();
            }
        }

        private void Load_Combo()
        {
            DataSet ds = new DataSet("ds");
            //this.hanghoa.Get_List = this.kh.id_kh;
            ds = this.kho.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                //this.count = 0;
            }
            else
            {
                this.cboKhonhap.Properties.DataSource = ds.Tables[0];
                this.cboKhoxuat.Properties.DataSource = ds.Tables[0];
            }
        }

        private void Save_Info()
        {
            this.nk.id_hd = 0;
            this.nk.makho = Convert.ToInt32(this.cboKhonhap.EditValue.ToString().Trim());
            this.nk.manx = 1;
            this.nk.ngaynhap = DateTime.Now;
            this.nk.nguoinhap = this.user.id_nv;
            this.nk.noidung = this.txtNoidung.Text;
            this.nk.sohd = this.txtSoHD.Text;
            this.nk.ghichu = this.txtGhichu.Text;
            this.nk.khoxuat = Convert.ToInt32(this.cboKhoxuat.EditValue.ToString().Trim());
        }

        private void Load_Info()
        {
            this.txtNoidung.Text = this.nk.noidung;
            this.txtSoHD.Text = this.nk.sohd;
            this.txtGhichu.Text = this.nk.ghichu;
            this.cboKhoxuat.EditValue = this.nk.khoxuat;
            this.cboKhonhap.EditValue = this.nk.makho;
        }

        private void cboKhoxuat_EditValueChanged(object sender, EventArgs e)
        {
            this.Load_Data();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            this.Save_Info();
            this.nk.ExcuteProcedure(this.flag + 1);
            this.nk.Get_Info();
            //Thực hiện lưu các chi tiết nhập hàng về kho
            for (int i = 0; i < this.count; i++)
            {
                //if (Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim()) > 0)
                //{
                cs_erp_chitiet_nhapkhoInfo ct = new cs_erp_chitiet_nhapkhoInfo();
                ct.makho = Convert.ToInt32(this.cboKhonhap.EditValue.ToString().Trim());
                ct.nhan = 1;
                ct.id_lannhap = this.nk.id_lannhap;
                ct.mahh = Convert.ToInt32(this.gridView1.GetRowCellValue(i, grdcID).ToString().Trim());
                ct.soluong = Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim());
                //- Convert.ToDouble(this.gridView1.GetRowCellValue(i, gridColumn1).ToString().Trim());
                ct.dongia = Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim());
                ct.thanhtien = ct.soluong * ct.dongia;
                ct.hangdu = false;
                ct.ExcuteProcedure(1);
                ct.makho = Convert.ToInt32(this.cboKhoxuat.EditValue.ToString().Trim());
                ct.nhan = -1;
                ct.ExcuteProcedure(1);
            }
            XtraMessageBox.Show("Lập chứng từ Xuất kho thành công, chờ xác nhận!", "Thông báo");
            this.Dispose(true);
        }
    }
}